﻿#include"stdafx.h"
#include"SQLexpression.h"

bool SqlEx::ForDelete() {

	string db = CStringTostring(pSocket->cur_db);
	if (db == "") {
		pSocket->SendMSG("Please choose a database !");
		return 0;
	}
	string msg = "";
	vector<struct_where>wh;
	string tname;

	regex re1("(?<=delete from ).+(?= where )");
	regex re2("(?<=where ).+(?=END)");
	smatch dsm;

	if (regex_search(Ex, dsm, re1)) {
		tname = dsm.str();
	}
	else {
		pSocket->SendMSG("请检查delete与where语句间是否存在错误!!");
		return false;
	}
	if (!authority->Auth_Judge(user_host, db, tname)) {
		pSocket->SendMSG("You do not have permission to  delete from the table !");
		return false;
	}
	if (regex_search(Ex, dsm, re2)) {
		string test = dsm.str();
		test = trimAll(test, " ");
		where(test, wh);
	}
	else {
		pSocket->SendMSG("请检查where语句之后是否存在错误!!");
		return false;
	}
	serverDB->Table_Delete(tname.c_str(), db, wh, msg);
	pSocket->SendMSG(msg);
	return true;
}

